﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Master/default.Master" AutoEventWireup="true"
    CodeBehind="Monthly-Zonal-Plan.aspx.cs" Inherits="NRCP.Monthly_Zonal_Plan" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="Scripts/jquery.tmpl.js" type="text/javascript"></script>
      <script src="Scripts/jquery.jstepper.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var summaryStore;
        var isAllSaveAllowed = false;
        $(document).ready(function () {
            LoadSummary();
            $(".text-quantity").jStepper({ minValue: 0, maxValue: 1000, minLength: 2 });
            $(".text-required").jStepper({ minValue: 1, maxValue: 1000, minLength: 2 });
        });
        function LoadSummary() {
            var planId = "<%= NRCP.Helpers.SiteUtility.IntQueryParam %>";
            var divId = "<%= DivisionId %>";
            var fishId = "<%= FishId %>";
            var params = "{'nationalPlanId':" + planId + ",'divisionId': " + divId + ",'fishId':" + fishId + "}";
            $.ajax({
                type: "POST",
                url: "App_Service/SiteDataUtility.asmx/LoadPlanSummaryByDivisionAndFish",
                data: params,
                beforeSend: function () { $("#wait").show(); },
                complete: function () { $("#wait").hide(); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    summaryStore = data.d;
                    $("#divPlanSummary").html("");
                    BindPlanSummary();

                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }
        function BindPlanSummary() {
            $("#divPlanSummary").html("");
            $.each(summaryStore, function (i, item) {
                var col1 = "<div class='blocks border1 grid_3'><div class='dash-bar-1-value'>" + item.Value + "</div><div class='dash-bar-1-title'>" + item.Key + "</div></div> ";
                $("#divPlanSummary").append(col1);
                if (parseInt(item.Value) == 0) {
                    isAllSaveAllowed = true;
                }
                else {
                    isAllSaveAllowed = false;
                }

            });

            AllowAllSave();
        }

        function AllowAllSave() {

            if (isAllSaveAllowed) {
                $("#btnSaveAll").show();
            }
            else {
                $("#btnSaveAll").hide();
            }
        }

        function IsValidQuantity(divName, total) {
            var result = true;

            $.each(summaryStore, function (i, item) {
                if (item.Key == divName) {
                    if (parseInt(item.Value) >= parseInt(total)) result = true;
                    else result = false;
                }
            });

            return result;
        }

        function ResetSummary(divName, total) {
            $.each(summaryStore, function (i, item) {
                if (item.Key == divName) {
                    item.Value = parseInt(item.Value) - parseInt(total);
                }
            });
            BindPlanSummary();
        }


        function LoadDistrict() {
            var planId = "<%= NRCP.Helpers.SiteUtility.IntQueryParam %>";
            var divId = "<%= DivisionId %>";
            var params = "{'nationalPlanId':" + planId + ",'divisionId': " + divId + "}";
            $.ajax({
                type: "POST",
                url: "App_Service/SiteDataUtility.asmx/LoadDistrictByPlanDivision",
                data: params,
                beforeSend: function () { $("#wait").show(); },
                complete: function () { $("#wait").hide(); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var store = data.d;
                    $("#selDistrict").html("");
                    $.each(store, function (i, item) {
                        var col1 = $("<option value='" + item.Value + "'>").append(item.Key).append("</option>");
                        col1.appendTo("#selDistrict");
                    });

                    LoadUpazila($("#selDistrict option:first").val());
                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }

        function LoadUpazila(selection) {
            var params = "{'districtId': " + selection + "}";
            $.ajax({
                type: "POST",
                url: "App_Service/SiteDataUtility.asmx/LoadUpazila",
                data: params,
                beforeSend: function () { $("#wait").show(); },
                complete: function () { $("#wait").hide(); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var store = data.d;
                    $("#selUpazila").html("");
                    $.each(store, function (i, item) {
                        var col1 = $("<option value='" + item.Value + "'>").append(item.Key).append("</option>");
                        col1.appendTo("#selUpazila");
                    });


                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }

        function saveplan() {


            var chemicalGroup = $("#<%= ddlChemicalGroup.ClientID %> option:selected").text();
            var chemicalGroupId = $("#<%= ddlChemicalGroup.ClientID %> option:selected").val();
            var qty = $("#txtCount").val();
            if (!IsValidQuantity(chemicalGroup, qty)) {
                alert("Total Can Not Excced Total Available");
                return;
            }
            var planId = "<%= NRCP.Helpers.SiteUtility.IntQueryParam %>";
            var divId = "<%= DivisionId %>";
            var fishId = "<%= FishId %>";
            var params = "{'nationalPlanId': " + planId + ",'divId':" + divId + ",'fishId':" + fishId + ",'chemicalGroupId':" + chemicalGroupId + ",'chemicalGroup':'" + chemicalGroup + "','upazilaId':" + $("#selUpazila option:selected").val() + ",'upazilaName':'" + $("#selUpazila option:selected").text() + "','monthId':" + $("#<%= ddlMonth.ClientID %> option:selected").val() + ",'monthName':'" + $("#<%= ddlMonth.ClientID %> option:selected").text() + "','count':" + qty + "}";
            $.ajax({
                type: "POST",
                url: "App_Service/SiteDataUtility.asmx/SaveMonthlyPlan",
                data: params,
                beforeSend: function () { $("#wait").show(); },
                complete: function () { $("#wait").hide(); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var store = data.d;
                    $("#repItems").html("");
                    $.each(store, function (i, item) {
                        var col1 = $("<td>").append(item.Division).append("</td>");
                        var col2 = $("<td>").append(item.Month).append("</td>");
                        var col3 = $("<td>").append(item.Count).append("</td>");

                        $("<tr>").append(col1).append(col2).append(col3).append("</tr>").appendTo("#repItems");

                    });

                    ResetSummary(chemicalGroup, qty);
                },
                error: function (msg) {
                    alert(msg);
                }
            });

        }

        function SaveAll() {
            var planId = "<%= NRCP.Helpers.SiteUtility.IntQueryParam %>";
            var divId = "<%= DivisionId %>";
            var fishId = "<%= FishId %>";
            var params = "{'nationalPlanId': " + planId + ",'divId':" + divId + ",'fishId':" + fishId + "}";
            $.ajax({
                type: "POST",
                url: "App_Service/SiteDataUtility.asmx/SaveMonthlyPlanAll",
                data: params,
                beforeSend: function () { $("#wait").show(); },
                complete: function () { $("#wait").hide(); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var store = data.d;
                    if (store == true) {
                        window.location = "<%= NRCP.Helpers.SiteSettings.BaseUrl %>Zonal-Plan.aspx?params=<%= NRCP.Helpers.SiteUtility.QueryParam %>"
                    }
                },
                error: function (msg) {
                    alert(msg);
                }
            });
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
    </asp:ScriptManager>
    <div id="wait" class="progress">
        <div class="progress-container">
            <div class="progress-header">
                Loading, please wait...</div>
            <div class="progress-body">
                <img src="<%= NRCP.Helpers.SiteSettings.BaseUrl %>App_Themes/css/images/pre-loader.gif" />
            </div>
        </div>
    </div>
    <br />
    <br class="clear" />
    <div class="grid_4 report-title-sub clarfix alpha">
        Total avaialble
    </div>
    <br class="clear" />
    <div class="grid_20 dash-bar-4 alpha">
        <div id="divPlanSummary" class="dash-bar-2 block-gray">
        </div>
    </div>
    <br class="clear" />
    <div class="grid_10 report-title-sub clarfix alpha">
        Monthly Plan Breakdown for
        <asp:Literal ID="litGroupName" runat="server"></asp:Literal>
    </div>
    <br class="clear" />
    <br />
    <asp:UpdatePanel ID="upnlUpazila" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
        <ContentTemplate>
            <div class="grid_10 alpha">
                <fieldset>
                    <legend>Add Monthly Plan</legend>
                    <label>
                        Month</label>
                    <br />
                    <asp:DropDownList ID="ddlMonth" runat="server">
                    </asp:DropDownList>
                    <br />
                    <label>
                        ChemicalGroup</label>
                    <br />
                    <asp:DropDownList ID="ddlChemicalGroup" runat="server">
                    </asp:DropDownList>
                    <br />
                    <label>
                        District</label>
                    <br />
                    <asp:DropDownList ID="ddlDistrics" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlDistrics_SelectedIndexChanged">
                    </asp:DropDownList>
                    <br />
                    <label>
                        Upazila</label>
                    <br />
                    <asp:DropDownList ID="ddlUpazila" runat="server">
                    </asp:DropDownList>
                    <br />
                    <label>
                        Total</label>
                    <br />
                    <asp:TextBox ID="txtCount" runat="server" CssClass="text-required"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvtxtCount" ControlToValidate="txtCount" SetFocusOnError="true"
                        runat="server" ErrorMessage="* required"></asp:RequiredFieldValidator>
                    <br />
                    <asp:Button ID="btnAdd" runat="server" Text="Add" CssClass="button medium blue" OnClick="btnAdd_Click" />
                </fieldset>
            </div>
            <div class="grid_10 omega">
                <table id="tblZonalPlan" width="100%" class="mGrid">
                    <thead>
                        <tr>
                            <th>
                                Upazila
                            </th>
                            <th>
                                Month
                            </th>
                            <th>
                                Count
                            </th>
                        </tr>
                    </thead>
                    <tbody id="repItems">
                        <asp:Repeater ID="repSchedules" runat="server">
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <%# Eval("Division")%>
                                        <asp:HiddenField ID="hidDivisionId" runat="server" Value='<%# Eval("DivisionId")%>' />
                                    </td>
                                    <td>
                                        <%# Eval("Month")%>
                                        <asp:HiddenField ID="hidGroupId" runat="server" Value='<%# Eval("GroupId")%>' />
                                        <asp:HiddenField ID="hidMonthId" runat="server" Value='<%# Eval("MonthId")%>' />
                                    </td>
                                    <td>
                                        <asp:TextBox ID="txtSampleCount" runat="server" CssClass="text-quantity" Text='<%# Eval("Count")%>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfvtxtSampleCount" ControlToValidate="txtSampleCount" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tbody>
                </table>
                <br />
                <asp:Button ID="btnSaveAll" runat="server" Text="Save All & Next" 
                    CssClass="button medium green"  Visible="false" onclick="btnSaveAll_Click"/>
                
            </div>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlDistrics" EventName="selectedindexchanged" />
            <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
</asp:Content>
